<?php

namespace app\common\service\driver;

use app\common\constant\driving\DriverMoneyLogConstant;
use app\common\model\driving\Driver;
use app\common\model\driving\DriverFeedback;
use app\common\model\driving\DriverLocation;
use app\common\model\driving\DriverMoneyLog;
use app\common\model\driving\DriverQrcode;
use app\common\model\driving\Order;
use hichefu\Api;
use think\cache\driver\Redis;
use think\Db;
use think\Exception;

/**
 * 司机服务类
 **/
class DriverService
{

    /**
     * 个人中心
     *
     * @param mixed $user 用户信息
     * @return array
     */
    public static function index($user)
    {
        $is_recharge = (integer)config('site.is_recharge');
        $order_receive_money = config('site.order_receive_money');
        $order_receive_money_text = '当余额少于' . $order_receive_money . '元时无法接单';
        $data = compact('is_recharge', 'order_receive_money', 'order_receive_money_text');
        $user = array_merge($user->toArray(), $data);
        return $user;
    }

    /**
     * 微信授权
     *
     * @param array $params
     * @param mixed $user 用户信息
     * @return string
     **/
    public static function get_openid($params, $user)
    {
        try {
            $user->save(['openid' => $params['openid'], 'unionid' => $params['unionid']]);
        } catch (Exception $e) {
            except($e->getMessage());
        }
        return true;
    }

    /**
     * 更新金额
     *
     * @param array $log_data
     * @param integer $user_id 用户ID
     * @param mixed $user 用户信息
     * @param string $money_type 类型:money=余额,freeze_money=冻结金额
     * @return bool
     */
    public static function money($log_data, $user_id, $user, $money_type)
    {
        $money = $log_data['money'];
        $change_type = $log_data['change_type'];

        if (!$user) {
//            $user = Driver::lock(true)->field('id,money,freeze_money')->find($user_id);
            $user = Driver::field('id,money,freeze_money')->find($user_id);
        }
        if ($user && $log_data['money'] != 0) {
            if ($money_type == 'money') {
                $user->money = $change_type == DriverMoneyLogConstant::CHANGE_TYPE_1 ? bcadd($user->money, $money, 2) : bcsub($user->money, $money, 2);
            } else {
                $user->freeze_money = $change_type == DriverMoneyLogConstant::CHANGE_TYPE_1 ? bcadd($user->freeze_money, $money, 2) : bcsub($user->freeze_money, $money, 2);
            }
            $user->save();
            DriverMoneyLog::create($log_data);
        }
        return true;
    }

    /**
     * 更新定位
     *
     * @param array $params
     * @param string $lng_lat 经纬度
     * @param string $order_id 订单号
     * @param mixed $user 用户信息
     * @return bool
     **/
    public static function location($params, $lng_lat, $order_id, $user)
    {
        if (!$params) {
            $params = explode(',', $lng_lat);
            $params = [
                'lng' => $params[0],
                'lat' => $params[1],
            ];
        }
        try {
            DriverLocation::where('driver_id', $user['id'])->update($params);
            if (!empty($order_id)) {
                $order = Order::where('status', Api::STAGE_13)
                    ->where('order_id', $order_id)
                    ->where('is_end', 0)
                    ->value('id');
                if ($order) {
                    $options = \think\Config::get('redis');
                    $redis = new Redis($options);
                    $key = $options['prefix'];
                    $redis->handler()->rpush($key . $order_id, $lng_lat); // 在列表中添加一个或多个值到列表尾部
                }
            }
        } catch (Exception $e) {
            except($e->getMessage());
        }
        return true;
    }

    /**
     * 收款码
     *
     * @param mixed $user 用户信息
     * @return mixed
     **/
    public static function index_qrcode($user)
    {
        return DriverQrcode::where('driver_id', $user['id'])->find();
    }

    /**
     * 提交收款码
     *
     * @param array $params
     * @param mixed $user 用户信息
     * @return bool
     */
    public static function create_qrcode($params, $user)
    {
        try {
            DriverQrcode::where('driver_id', $user['id'])->update($params);
        } catch (Exception $e) {
            except($e->getMessage());
        }
        return true;
    }

    /**
     * 意见反馈
     *
     *  @param array $params
     * @param mixed $user 用户信息
     * @return bool
     **/
    public static function feedback($params, $user)
    {
        try {
            $data = $params;
            $data['user_id'] = $user['id'];
            DriverFeedback::create($data);
        } catch (Exception $e) {
            except($e->getMessage());
        }
        return true;
    }

}
